{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Modern Portfolio Theory\n",
    "\n",
    "It was formulated in the 1950's by Markowitz (award nobel prize for it)\n",
    "\n",
    "**What was the main idea?**\n",
    "A single stock is quite unpredictable: we do not know for certain whether the price will go up or down. **But** _we may combine several stocks_ in order to reduce the risk as much as possible!\n",
    "\n",
    "This is called **DIVERSIFICATION**\n",
    "\n",
    "Combining assests is the main idea: it is the same for Black-Scholes Model!\n",
    "\n",
    "The idea is the any loss is one stock is offest by the gain in other stocks in the portfoilo\n",
    "\n",
    "_*The model has several assumptions*_\n",
    "    \n",
    "    1) The Returns are normally distributed\n",
    "        - To describe normal distributions we need mean (mew) and variance(omega) exclusively\n",
    "    \n",
    "    2) Investors are risk-averse: investors will take on more risk if they are  expecting more reward\n",
    "\n",
    "*As always less risk less return*\n",
    "\n",
    "Modern Portfolio theory allows investors ro construct optimal portfolios offering maximum possible expected return for a given level of risk!\n",
    "\n",
    "**So what is an efficient portfolio?** \n",
    "This is a portfolio that has the highest reward for a given level of risk OR te lowest level of risk for a given reward! ng available stocks in a way that all positions are long\n",
    "\n",
    "\n",
    "# Mathimatical Formulation\n",
    "\n",
    "The investors are not allowed to set up short positions in a security\n",
    "\n",
    "So 100% of the wealth has to be divided amoung all options \n",
    "*For example: **APPL, GOOGL, TSLA, GE***\n",
    "\n",
    "    stock:     APPL   GOOGL   TSLA   GE\n",
    "    Precent:   20%    30%     25%    25%\n",
    "    Weight:    0.2    0.3     0.25   0.25\n",
    "    Weights = [0.2,0.3,0.25,0.25] = 1 (100%)\n",
    "    \n",
    "Above is how an investor might split amounst all avaiable assets\n",
    "\n",
    "### Formulation:\n",
    "\n",
    "\n",
    "$w_{i}$ \\- weight of the i\\-th stock\n",
    "\n",
    "$r_{i}$ \\- return of the i\\-th stock *calculated based on historical data*\n",
    "\n",
    "$u_{i}$ \\- expected return for security i *it is mean more or less*\n",
    "\n",
    "***How to calculate the return?***\n",
    "\n",
    "We can calculate the return on a day by day basis with:\n",
    "\n",
    "*Daily Return*\n",
    "\n",
    "```\n",
    "   ((stockPrice_n - stockPrice_n-1) / stockPrice_n-1) x 100 // returns %\n",
    "```\n",
    "\n",
    "***Usually we use the natural logarithm as the return!\n",
    "\n",
    "`log(((stockPrice_n - stockPrice_n-1) / stockPrice_n-1))`\n",
    "\n",
    "*We use log of return instead of actual prices of stocks as a form of normalization: Important for machine learning techniques and statistical analysis*\n",
    "\n",
    "`log((stockPrice_n / stockPrice_n-1) - 1)`\n",
    "\n",
    "*Using the log allows for faster algo's*\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Expected return of a morkowitz model portfolio\n",
    "**Not a dynamic model, not ideal**\n",
    "\n",
    "$w_{i}$ \\- weight of the i\\-th # stock\n",
    "\n",
    "$r_{i}$ \\- return of the i\\-th stock # *calculated based on historical data*\n",
    "\n",
    "$u_{i}$ \\- expected return for security i # *it is mean more or less*\n",
    "\n",
    "*This model relies heavily on historical data. Historical mean performance is assumed to be the best estimator for future (expected) performance*\n",
    "\n",
    "$\\mu_{porfolio} = E($$\\sum_{i}w_{i}r_{i}$$)=$$\\sum_{i}w_{i}E(r_{i})$$=$$\\sum_{i}w_{i}\\mu_{i}$$ = \\underline{w}^{T} \\underline{\\mu}$\n",
    "\n",
    "***This is the expected return of the portfolio!***"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<hr style=\"height: 5px;\">\n",
    "## Risk and Return of a morkowitz model portfolio\n",
    "\n",
    "$\\underline{What~about~the~risk~of~the~portfolio?}$\n",
    "\n",
    "- The risk has something to do with the volatility, which has something to do with standard deviation and varience!\n",
    "\n",
    "Equation: $\\sigma_{ij} = E[(r_{i} - \\mu_{j})(r_{j}-\\mu_{i})]$  // *covariance*\n",
    "\n",
    "- Test: What is volatility? A - volatility is the measurement of dispersion of expected returns in a security\n",
    "\n",
    "**Covariance** measures how much TWO stocks vary together\n",
    "\n",
    "$\\rightarrow \\sigma_{ij} < 0$ // A negative covarience means returns have inverse relationship\n",
    "\n",
    "$\\rightarrow \\sigma_{ij} > 0$ // A postive covarience means returns are correlated \n",
    "\n",
    "**Markowitz's Theory** is abot diversifiation: Processing stocks with high positice covariance does NOT provide very much diversification!\n",
    "  - The aim of diversification is to eliminate fluctuations in the long term\n",
    "  \n",
    "**Variance** measures how much variation is in ONE stock\n",
    "\n",
    "Equation: $\\sigma_{i}^{2} = E[(r_{i} - \\mu_{i})^2]$  // *variance*\n",
    "\n",
    "- For calculating the variance of the port we need the covariance matrix of the stocks involved in the portfolio\n",
    "\n",
    "$\\Sigma =$ \n",
    "$\n",
    "\\begin{bmatrix}\n",
    "    \\sigma^{2}_{1}  & \\dots  & \\sigma_{1n} \\\\\n",
    "    \\vdots & \\ddots & \\vdots \\\\\n",
    "    \\sigma_{n1} & \\dots  & \\sigma^{2}_{n}\n",
    "\\end{bmatrix}\n",
    "$\n",
    "\n",
    "This covariance matrix contains the relationship between all the stocks in the port\n",
    "\n",
    "***Expected Port Variance***\n",
    "\n",
    "$\\sigma_{portfolio}^{2} = E[(r_{i}-\\mu_{i})^{2}] = \\Sigma_{i}\\Sigma_{j}w_{i}w_{j}\\sigma_{ij} \\\\  \\sigma_{portfolio}^{2} =\\underline{w}^{T}\\underline{\\Sigma}\\underline{w}$\n",
    "\n",
    "$\\sigma_{portfolio}= \\sqrt{\\underline{w}^{T}\\underline{\\Sigma}\\underline{w}}$ is linear algebra calculation which is a fast vectorized formula\n",
    "\n",
    "*Python implementation*:\n",
    "`portfolio_volatility = np.sqrt(np.dot(weights.T,np.dot(returns.cov()*252,weights))) \n",
    "`\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<hr style=\"height: 5px;\">\n",
    "## The Expected Return vs Expected Risk\n",
    "\n",
    "<img src=\"images/markowitz_port_optimization.png\">\n",
    "\n",
    "The Dots represent different $w$ weight distrubitions $\\rightarrow$ different portfolio stock distrubitions\n",
    "\n",
    "An investor is interested in\n",
    "\n",
    "1. The maximum return given a fixed risk level\n",
    "2. The minimum risk given a fixed return\n",
    "\n",
    "These portfolios make up the so-called: ***Efficient-frontier***\n",
    "\n",
    "<img src=\"images/efficientfrontier.png\">\n",
    "\n",
    "This is the main feature of the Markowitz model: the investor can decide the risk or the expected return\n",
    "\n",
    "*Remember:* If you want to make money, you have to take risk!"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "<hr style=\"height: 5px;\">\n",
    "## Sharpe-Ratio\n",
    "\n",
    "***What is the Sharpe-ratio?***\n",
    "\n",
    "*It is one of the most import risk/return measures used in finance William Sharpe used this parameter!*\n",
    "\n",
    "1. It describes how much excess return you are receiving for extra volatility that you endure holding a riskier asset/stock\n",
    "\n",
    "2. It measures the excess return (risk oremium) per uint of standard deviation on an asset(s)\n",
    "\n",
    "<h3>Sharpe-ratio: $S(x) = \\frac{r_{x} - R_{f}}{StdDev(x)}$ </h3>\n",
    "- $r_{x}$: average rate of return of investment x\n",
    "- $R_{f}$: rate of return of risk-free security\n",
    "\n",
    "*A Sharpe-Ratio **S(x) > 1** is considered to be good*\n",
    "\n",
    "Relating back to the Vol vs Return, the best portfolio will be:\n",
    "- On the Efficient Frontier line\n",
    "- Be the best balance of StdDev (Volatility) and Rate of Return\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<hr style=\"height: 5px;\">\n",
    "## Capital Allocation Line\n",
    "\n",
    "<img src=\"images/cal.png\">\n",
    "\n",
    "Investors can have risk-free assets as well usually: for example treasury bills\n",
    "\n",
    "Consider this fact what is the optimal portfolio now?\n",
    "\n",
    "The optimal portfolios lie on the capital allocation line!\n",
    "\n",
    "<img src=\"images/cml2.jpg\">\n",
    "\n",
    "<img src=\"images/sml.jpg\">"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "### Efficient portfolios (portfolios that have maximum return for a given risk, or lowest level of risk for a fixed return)  are on the capital allocation line!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
